package org.jack.view;

import java.io.IOException;
import java.io.InputStream;

import org.jack.bluetooth.BluetoothClient;
import org.jack.input.SwitchEvent;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;

public class House extends SurfaceView implements Runnable, OnTouchListener
{
	private SurfaceHolder holder = null;
	private volatile boolean running = false;
	private Thread renderThread = null;
	private AssetManager assets = null;
	private Bitmap map = null, door = null, open_light = null, close_light = null,
				close_fan = null, low_gear_fan = null, height_gear_fan = null;
	private int height, width;
	private volatile boolean openLeftLight = false, openRightLight = false;
	private int fan_state = 1;
	private float left_light_x, left_light_y, fan_x, fan_y, right_light_x, right_light_y, door_x, door_y;
	private BluetoothClient client = null;
	private Context context = null;
	public House(Context arg_context, BluetoothClient arg_client) 
	{
		super(arg_context);
		context = arg_context;
		client = arg_client;
		holder = getHolder();
		assets = context.getAssets();
		map = createMap("background.png");
		width = map.getWidth();
		height = map.getHeight();
		door = createMap("door.png");
		open_light = createMap("open_light.png");
		close_light = createMap("close_light.png");
		close_fan = createMap("close_fan.png");
		low_gear_fan = createMap("low_gear_fan.png");
		height_gear_fan = createMap("height_gear_fan.png");
		setOnTouchListener(this);
	}

	private Bitmap createMap(String file)
	{
		InputStream is = null;
		Bitmap tmp = null;
		try 
		{
			is = assets.open(file);
			tmp = BitmapFactory.decodeStream(is);
		} catch (IOException e) 
		{
			e.printStackTrace();
		} finally
		{
			if (null != is)
			{
				try
				{
					is.close();
				} catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}
		return tmp;
	}
	public void resume()
	{
		running = true;
		renderThread = new Thread(this);
		renderThread.start();
	}
	
	public void pause()
	{
		running = false;
		try
		{
			renderThread.interrupt();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void dispose()
	{
		map.recycle();
		door.recycle();
		open_light.recycle();
		close_fan.recycle();
		close_light.recycle();
		low_gear_fan.recycle();
		height_gear_fan.recycle();
	}
	@Override
	public void run() 
	{
		while (running)
		{
			if (!holder.getSurface().isValid())
			{
				continue;
			}
			Canvas canvas = holder.lockCanvas();
			canvas.drawBitmap(map, 0, 0, null);
			door_x = -(door.getWidth() / 3);
			door_y = height/4;
			canvas.drawBitmap(door, door_x, door_y, null);
			left_light_x = width / 2;
			left_light_y = height/4 + 60;
			if (openLeftLight)
			{
				canvas.drawBitmap(open_light, left_light_x, left_light_y, null);
			} else
			{
				canvas.drawBitmap(close_light, left_light_x, left_light_y, null);
			}
			fan_x = left_light_x + close_light.getWidth() + 20;
			fan_y = left_light_y;
			int fs = fan_state%3;
			if (fs == 1)
			{
				canvas.drawBitmap(close_fan, fan_x, fan_y, null);
			} else if (fs == 2)
			{
				canvas.drawBitmap(low_gear_fan, fan_x, fan_y, null);
			} else
			{
				canvas.drawBitmap(height_gear_fan, fan_x, fan_y, null);
			}
			
			right_light_x = fan_x + close_fan.getWidth() + 20;
			right_light_y = left_light_y;
			if (openRightLight)
			{
				canvas.drawBitmap(open_light, right_light_x, right_light_y, null);
			} else
			{
				canvas.drawBitmap(close_light, right_light_x, right_light_y, null);
			}
			holder.unlockCanvasAndPost(canvas);
		}
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) 
	{
		if(arg1.getAction() == MotionEvent.ACTION_DOWN)
		{
			float x = arg1.getX();
			float y = arg1.getY();
			if (x > left_light_x && x < left_light_x + close_light.getWidth() &&
					y > left_light_y && y < left_light_y + close_light.getHeight())
			{
				openLeftLight = !openLeftLight;
				if (openLeftLight)
				{
					client.sendMsg(SwitchEvent.OPEN_LEFT_LIGHT);
				} else
				{
					client.sendMsg(SwitchEvent.CLOSE_LEFT_LIGHT);
				}
				return false;
			}
			if (x > right_light_x && x < right_light_x + close_light.getWidth() &&
					y > right_light_y && y < right_light_y + close_light.getHeight())
			{
				openRightLight = !openRightLight;
				if (openRightLight)
				{
					client.sendMsg(SwitchEvent.OPEN_RIGHT_LIGHT);
				} else
				{
					client.sendMsg(SwitchEvent.CLOSE_RIGHT_LIGHT);
				}
				return false;
			}
			
			if (x > fan_x && x < fan_x + close_fan.getWidth() &&
					y > fan_y && y < fan_y + close_fan.getHeight())
			{
				 fan_state++;
				 switch (fan_state%3)
				 {
				 case 0:
					 client.sendMsg(SwitchEvent.HIGH_GEAR_FAN);
					 break;
				 case 1:
					 client.sendMsg(SwitchEvent.CLOSE_FAN);
					 break;
				 case 2:
					 client.sendMsg(SwitchEvent.LOW_GEAR_FAN);
					 break;
				 }
				 return false;
			}
			if (x > door_x && x < door_x + door.getWidth() &&
					y > door_y && y < door_y + door.getHeight())
			{
				final EditText inputServer = new EditText(context);
		        AlertDialog.Builder builder = new AlertDialog.Builder(context);
		        
		        builder.setTitle("请输入密码").
		        setIcon(android.R.drawable.ic_dialog_info).setView(inputServer).
		        setPositiveButton("确定", new DialogInterface.OnClickListener()
		        {

		        	@Override
		            public void onClick(DialogInterface dialog, int which) 
		            {
		               if ("123456".equals(inputServer.getText().toString()))
		               {
		          	 		client.sendMsg(SwitchEvent.CLICK_DOOR);
		               } else
		               {
		            	   Toast.makeText(context, "密码错误,请重新输入", Toast.LENGTH_SHORT).show();
		               }
		             }

		        }).setNegativeButton("取消", null);
		        builder.show();
			
				 return false;
			}
			
		}
		return false;
	}
}
